<?php

//默认编码为UTF-8
error_reporting(0);
session_start();
$tabPre = 'pf_';
$conn = mysql_connect('localhost','root','123456') or die('不能连接mysql!');
mysql_select_db('gather') or die('不能选择数据库');
mysql_query('set names utf8');
header("Content-type: text/html; charset=utf-8"); 

set_time_limit(0);
set_magic_quotes_runtime(0);

//对字符串进行转义
function ds($string, $force = 0) {
	!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
	if(!MAGIC_QUOTES_GPC) {
		if(is_array($string)) {
			foreach($string as $key => $val) {
				$string[$key] = ds($val, $force);
			}
		} else {
			$string = addslashes($string);
		}
	}
	return $string;
}
$_GET  = ds($_GET);
$_POST = ds($_POST);




//把用户输入的字符串转化成php能识别的正则因子
function getPat($string){
	$string=str_replace('\\','\\\\',$string);
	$string=str_replace("(","\(",$string);
	$string=str_replace(")","\)",$string);
	$string=str_replace("[","\[",$string);
	$string=str_replace("]","\]",$string);
	$string=str_replace('"','\"',$string);
	$string=str_replace('.','\.',$string);
	$string=str_replace('?','\?',$string);
	$string=str_replace('$','\$',$string);
	$string=str_replace('^','\^',$string);
	$string=str_replace('/','\/',$string);
	$string=str_replace('+','\+',$string);
	$string=preg_replace("/\{(.*?)\}/eis","replace_preg('\\1')",$string);
	return $string;
}

function replace_preg($string){
	$string=str_replace('\"','"',$string);
	$rule=$string;
	
	if(ereg("^NO",$rule)){
		$detail=explode("NO",$rule);
		return "([^{$detail[1]}]*)";
	}elseif($rule=='*'){
		return "(.*?)";
	}elseif($rule=='**'){
		return "(.*)";
	}else{
		$detail=explode("=",$string);
		$rule=$detail[1];
		if(ereg("^NO",$rule)){
			$detail=explode("NO",$rule);
			return "([^{$detail[1]}]*)";
		}elseif($rule=='*'){
			return "(.*?)";
		}elseif($rule=='**'){
			return "(.*)";
		}
	}
}

function gc($url,$fromChartSet='UTF-8'){ 
 
	//while($cnt < 3 && ($result=@file_get_contents($url))===FALSE) $cnt++;
	$curl = curl_init($url); //初始化   
	curl_setopt($curl, CURLOPT_HEADER, FALSE);     
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);  //将结果输出到一个字符串中，而不是直接输出到浏览器     
	curl_setopt($curl, CURLOPT_REFERER, getDomain($url) ); //最重要的一步，手动指定Referer    
	$result = curl_exec($curl); //执行    
	if (curl_errno($curl)) {   
		curl_close($curl);    
		return NULL; 
	}    
	curl_close($curl);  


	$fromChartSet = strtoupper($fromChartSet);
	if($fromChartSet != 'UTF-8'){
		$result	= mb_convert_encoding($result,'UTF-8',$fromChartSet);
		//$result	= iconv($fromChartSet,'UTF-8',$result);
	}
	return  $result;
}

/*
	功能：获取绝对地址
	$rootUrl 为采集的网页，即基础页
	$url     匹配得到的详细页网址，有
		全绝对路径(http:// 开始的，如 http://www.phpfull.com/news.php)，
		半绝对路径(/ 开始的 ，如/detail.php)，
		和相对路径(.. 或者 直接文件名的 ，如 ../news.php 或者news.php )
	// to do 如果是 .. 开始的还没有处理
*/
function getRealUrl($rootUrl,$url){
	if(!ereg("^http://",$url)){
		if(ereg("^/",$url)){
			$url=preg_replace("/http:\/\/([^\/]+)(.*)/is","http://\\1$url",$rootUrl);
		}elseif(ereg("^./",$url)){
			$url = ltrim($url,'.');
			$url=preg_replace("/http:\/\/([^\/]+)(.*)/is","http://\\1$url",$rootUrl);
		}elseif(ereg("^../",$url)){
			$url = rep2($rootUrl,$url);
			if(!ereg("^http://",$url)){
				$url = str_replace('http:/','http://',$url);
			}
		}else{
			$url=str_replace(basename($rootUrl),"",$rootUrl).$url;
		}
	}
	$pat = '/[\xe0-\xef][\x80-\xbf]{2}/';
	preg_match_all($pat,$url ,$m);
	$length = sizeof($m[0]);
	if($length>0){
		for($i=0;$i<$length;$i++){
			$key = $m[0][$i];
			$url = str_replace($key,urlencode($key),$url);
		}
	}
	return $url;
}
/** 
* 计算绝对路径 
* @param 当前路径 $cur 
* @param 相对路径 $relative 
*/
function rep2($cur,$relative){  
    $cur=str_replace('//','/',$cur);  
    $relative=str_replace('//','/',$relative);  
    $curArr=explode('/',$cur);  
    $reArr=explode('/',$relative);  
    $curlen=count($curArr);  
    $relen=count($reArr);  
    $base=dirname($cur);  
    if($reArr[0]=='..'){  
        foreach ($reArr as $val){  
            if($val=='..') {  
                $base=dirname($base);  
                continue;  
            }  
            return $base.'/'.str_replace('../','',$relative);  
        }  
    }elseif($reArr[0]==''){  
        return $relative;  
    }else{  
        if($reArr[0]=='.'){  
            return $base.'/'.str_replace('./','',$relative);  
        }  
        return $base.'/'.$relative;  
    }  
}

function getDomain($url){
	$domain = '/';
	$domain=preg_replace("/http:\/\/([^\/]+)(.*)/is","http://\\1$domain",$url);
	return $domain;
}

//过滤字符串中超链接
function filterStr($str){
	$search = array ("'<a[^>]*?>.*?</a>'si");
	$replace = array ("");
	return preg_replace($search,$replace,$str);
}

//替换指定的字符串
function getReplace($str,$replace){
	$temp = explode("\r\n",$replace);
	$old = array();
	$new = array();
	foreach($temp as $v){
		$t = explode('|',$v);
		$old[] = $t[0];
		$new[] = $t[1];
	}
	return str_replace($old,$new,$str);
}




function p($message,$limit="\t"){
	echo $message.$limit;
	ob_flush();flush();
}
?>